package cn.b.sky.form.form.builder;

import cn.b.sky.form.form.builder.impl.ElementBuilder;
import cn.b.sky.form.model.ScreenField;
import cn.b.sky.form.model.Section;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <strong>Title : PanelBuilder.java<br></strong>
 * <strong>Package : cn.b.sky.form.form.builder<br></strong>
 * <strong>Description : </strong>板块HTML代码生成器<br> 
 * <strong>Create on : 2016年11月11日 下午4:13:47<br></strong>
 * <p>
 * @author ShiYing.Ke<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class PanelBuilder {

	public static String toHtmlElement(Section section) {

		StringBuilder panelHtml = new StringBuilder();
		panelHtml.append("<div class=\"panel\" style=\"display: block; margin: 0px\">");
		panelHtml.append("<div class=\"panelHeader\">");
		panelHtml.append("<div class=\"panelHeaderContent\">");
		panelHtml.append("<h1 style=\"cursor: move;\">");
		panelHtml.append(section.getSectionCode() != null ? section.getSectionCode() : "");
		panelHtml.append("</h1>");
		panelHtml.append("</div>");
		panelHtml.append("</div>");
		panelHtml.append("<div class=\"panelContent pageFormContent\">");
		if(section != null){
			List<ScreenField> fieldList = section.getScreenField();
			// 排序
			Collections.sort(fieldList, new Comparator<ScreenField>() {

				@Override
				public int compare(ScreenField o1, ScreenField o2) {
					int ord1 = o1.getFieldOrdinal() == null ? 0 : o1.getFieldOrdinal().intValue();
					int ord2 = o2.getFieldOrdinal() == null ? 0 : o2.getFieldOrdinal().intValue();
					return ord1 - ord2;
				}
			});
			for(ScreenField field : section.getScreenField()){
				panelHtml.append(ElementBuilder.getElementBuilder().buildHTMLElement(field));
			}
		}
		panelHtml.append("</div>");
		panelHtml.append("</div>");

		return panelHtml.toString();
	}	
}
